package com.zhangtai.modules.service.impl.insurance;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.dao.CredentialInfoDao;
import com.zhangtai.modules.dao.InsuranceDao;
import com.zhangtai.modules.dto.InsuranceDto;
import com.zhangtai.modules.entity.insurance.InsuranceEntity;
import com.zhangtai.modules.service.insurance.InsuranceService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;


@RestController
public class InsuranceServiceImpl extends ServiceImpl<InsuranceDao, InsuranceEntity> implements InsuranceService {

    @Autowired
    private InsuranceDao insuranceDao;

    @Override
    public PageResult queryPage(@RequestParam(value = "keyword", required = false) String keyword,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        IPage<InsuranceEntity> page = this.page(
                new Page<InsuranceEntity>(pageNum,pageSize),
                new QueryWrapper<InsuranceEntity>().like(!StringUtils.isEmpty(keyword),"name",keyword)
                        .eq("is_delete",0)
        );

        return new PageResult(page);
    }

    @Override
    public List<InsuranceEntity> getAllInsuranceList(){
        return this.list(new QueryWrapper<InsuranceEntity>().eq("is_delete",0).eq("status",1));
    }

    public PageResult<InsuranceDto> getInsuranceList(@RequestParam(value = "companyId", required = false) Long companyId,
                                                        @RequestParam(value = "insuranceUnit", required = false) String insuranceUnit,
                                                        @RequestParam(value = "status", required = false) Integer status,
                                                        @RequestParam(value = "startTime", required = false) String startTime,
                                                        @RequestParam(value = "endTime", required = false) String endTime,
                                                        @RequestParam(value = "number", required = false) Integer number,
                                                        @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        Page<InsuranceDto> page = new Page<>(pageNum,pageSize);
        List<InsuranceDto> list = insuranceDao.getInsuranceList(companyId,insuranceUnit,status,startTime,endTime,page);
        page.setRecords(list);
        return new PageResult<>(page);
    }

    @Override
    public InsuranceEntity getByIdE(Long id) {
            InsuranceEntity one = this.getOne(new QueryWrapper<InsuranceEntity>().eq("id", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public R saveE(InsuranceEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(InsuranceEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R unValidOne(Long id ){
        InsuranceEntity entity = new InsuranceEntity();
        entity.setId(id);
        entity.setStatus(2);
        entity.setNumber(0);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
            InsuranceEntity entity = new InsuranceEntity();
        entity.setId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

}